﻿using System.Data;
using System.Data.SQLite;
using System.Text;
using NDK.Module.Model;
using NDK.Utils;

namespace NDK.Module.DAL
{
    class MesServices
    {
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Mes model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update MesInfo set ");
            strSql.Append("URL=@URL,");
            strSql.Append("username=@username,");
            strSql.Append("password=@password,");
            strSql.Append("siteId=@siteId,");
            strSql.Append("operation=@operation,");
            strSql.Append("resource=@resource,");
            strSql.Append("dcgroup=@dcgroup");
 
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@URL", DbType.String,50),
					new SQLiteParameter("@username", DbType.String,30),
					new SQLiteParameter("@password", DbType.String,30),
                    new SQLiteParameter("@siteId", DbType.String,30),
					new SQLiteParameter("@operation", DbType.String,30),                       
                    new SQLiteParameter("@resource", DbType.String,30),
					new SQLiteParameter("@dcgroup", DbType.String,30),                      
                                           };
            parameters[0].Value = model.URL;
            parameters[1].Value = model.username;
            parameters[2].Value = model.password;
            parameters[3].Value = model.siteId;
            parameters[4].Value = model.operation;
            parameters[5].Value = model.resource;
            parameters[6].Value = model.dcgroup;

            int rows = SQLiteUtils.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 通过ID，得到一个对象实体
        /// </summary>
        public Mes GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select URL,username,password,siteId,operation,resource,dcgroup from MesInfo ");
            strSql.Append(" where rowid=@rowid ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@rowid", DbType.Int32,4)};
            parameters[0].Value = ID;

            Mes model = new Mes();
            DataSet ds = SQLiteUtils.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["URL"].ToString() != "")
                {
                    model.URL = ds.Tables[0].Rows[0]["URL"].ToString();
                }
                if (ds.Tables[0].Rows[0]["username"].ToString() != "")
                {
                    model.username =ds.Tables[0].Rows[0]["username"].ToString();
                }
                if (ds.Tables[0].Rows[0]["password"].ToString() != "")
                {
                    model.password = ds.Tables[0].Rows[0]["password"].ToString();
                }
                if (ds.Tables[0].Rows[0]["siteId"].ToString() != "")
                {
                    model.siteId = ds.Tables[0].Rows[0]["siteId"].ToString();
                }
                if (ds.Tables[0].Rows[0]["operation"].ToString() != "")
                {
                    model.operation = ds.Tables[0].Rows[0]["operation"].ToString();
                }
                if (ds.Tables[0].Rows[0]["resource"].ToString() != "")
                {
                    model.resource = ds.Tables[0].Rows[0]["resource"].ToString();
                }
                if (ds.Tables[0].Rows[0]["dcgroup"].ToString() != "")
                {
                    model.dcgroup = ds.Tables[0].Rows[0]["dcgroup"].ToString();
                }
               
                return model;
            }
            else
            {
                return null;
            }
        }
    }
}
